const solc = require('solc'), linker = require('solc/linker')
fs = require('fs');
// Web3 = require('web3');
// web3 = new Web3(new Web3.providers.WebsocketProvider('http://192.168.2.155:8546'))

// const MyContractJSON = JSON.parse(require('fs').readFileSync('./build/contracts/Token.json', 'utf8'));



// let input = require('fs').readFileSync('./contracts/Token.sol', 'utf8')
var input = {
    'maker.sol': fs.readFileSync('../contracts/maker.sol', 'utf8')
}
function findImports(path) {
    // console.log('path:', path);
    if (path === '/contracts/token/ERC20/StandardToken.sol')
        return { contents: fs.readFileSync('../contracts/token/ERC20/StandardToken.sol', 'utf8') }
    else if (path === '/contracts/token/ERC20/BasicToken.sol')
        return { contents: fs.readFileSync('../contracts/token/ERC20/BasicToken.sol', 'utf8') }
    else if (path === '/contracts/token/ERC20/ERC20.sol')
        return { contents: fs.readFileSync('../contracts/token/ERC20/ERC20.sol', 'utf8') }
    else if (path === '/contracts/token/ERC20/ERC20Basic.sol')
        return { contents: fs.readFileSync('../contracts/token/ERC20/ERC20Basic.sol', 'utf8') }
    else if (path === '/contracts/math/SafeMath.sol')
        return { contents: fs.readFileSync('../contracts/math/SafeMath.sol', 'utf8') }
    else
        return { error: 'File not found' }
}
var output = solc.compile({ sources: input }, 1, findImports);
let arr = []
for (var contractName in output.contracts) {
    let obj = {}
    // console.log(contractName.substring(contractName.indexOf('\\'),1) + ':===> ' + output.contracts[contractName].bytecode);
    // obj[contractName.split('\/')[contractName.split('\/').length - 1].split(':')[contractName.split(':').length - 1]] = contractName.split('\/')[contractName.split('\/').length - 1].split(':')[contractName.split(':').length - 1];
    // obj.bytecode = output.contracts[contractName].bytecode;

    obj[contractName.split('\/')[contractName.split('\/').length - 1].split(':')[contractName.split(':').length - 1]] = output.contracts[contractName].bytecode;
    arr.push(obj);

    // console.log(contractName.split('\/')[contractName.split('\/').length-1].split(':')[contractName.split(':').length-1]);
}

// console.log('arr==>', arr);
let bytecode='';
let bits=
{
    'SafeMath': '',
    'BasicToken': '',
    'ERC20': '',
    'ERC20Basic': '',
    'StandardToken': ''
}

// bytecode = linker.linkBytecode(bytecode,bits);

// console.log('bytecode==>', bytecode);